<?php

session_start();

// Include i file necessari
include('inc/config.inc.php');
include('inc/template.inc.php');
include('inc/db.inc.php');
include('inc/check.inc.php');

// Recupera l'id dell'utente
$user_id = $_SESSION['uid'];

$name		= "";
$surname	= "";
$bg			= "";
$desc		= "";
$affects	= "";
$pass		= "";
$img		= "";

$message_result = "";

// Recupera i dati del personaggio
if($r1 = $db -> query("SELECT user_name, user_surname, user_bg, user_affects, user_desc, user_image FROM users WHERE user_id = '$user_id'"))
{
	$rd1 = $r1 -> fetch_assoc();

	// Informazioni personaggio
	$name		.= $rd1['user_name'];
	$surname	.= $rd1['user_surname'];
	$bg			.= htmlentities($rd1['user_bg']);
	$desc		.= htmlentities($rd1['user_desc']);
	$affects	.= htmlentities($rd1['user_affects']);
	$img		.= htmlentities($rd1['user_image']);
	
	$new_desc		= (! empty($_POST['desc'])) ? trim(mysqli_real_escape_string($db, $_POST['desc'])) : '';
	$new_image		= (! empty($_POST['image'])) ? trim(mysqli_real_escape_string($db, $_POST['image'])) : '';
	$new_affects	= (! empty($_POST['affects'])) ? trim(mysqli_real_escape_string($db, $_POST['affects'])) : '';
	$new_bg			= (! empty($_POST['bg'])) ? trim(mysqli_real_escape_string($db, $_POST['bg'])) : '';
			
	// Se � stato modificato uno dei dati, li aggiorna nel database
	if( ($new_desc != '' AND $desc) OR ($new_image != '' AND $new_image != $img) OR ($new_affects != '' AND $new_affects != $affects) OR ($new_bg != '' AND $new_bg != $bg)) 
	{				
		$db -> query("UPDATE users SET user_bg = '$new_bg', user_desc = '$new_desc', user_image = '$new_image', user_affects = '$new_affects' WHERE user_id = '$user_id'");	
		$message_result	.= '<div class="alert alert-success">Profilo salvato</div>';
		
		$bg			= $new_bg;
		$desc 		= $new_desc;
		$affects	= $new_affects;
		$img		= $new_image;
		
	} else {
		
		$save = (! empty($_POST['save'])) ? $_POST['save'] : 0;
		
		if( $save == 1)
		{
			if($new_desc != $desc OR $new_desc != $desc OR $new_affects != $affects OR $new_image != $img)
			{
				$db -> query("UPDATE users SET user_bg = '$new_bg', user_desc = '$new_desc', user_image = '$new_image', user_affects = '$new_affects' WHERE user_id = '$user_id'");
				$message_result	.= '<div class="alert alert-success">Profilo salvato</div>';
				
				$bg			= $new_bg;
				$desc 		= $new_desc;
				$affects	= $new_affects;
				$img		= $new_image;
				
			} else {
				$message_result	.= '<div class="alert alert-default">Nessuna modifica</div>';
			}
		}
	}
	
	// Lista delle tag speciali
	$tags = array(
					'temp_title' => $g_name,'temp_image' =>$img, 'temp_name' => $name, 'temp_pass' => $pass,
			 		'temp_bg' => $bg, 'temp_surname' => $surname, 'temp_affects' => $affects, 'temp_desc' => $desc, 
			 		'temp_message' => $message_result
				);

	// Carica il template e formatta le tag speciali
	$temp = new template('style/avatar-edit.template.html');
	$temp -> replace($tags);

	// Visualizza l'output
	echo $temp -> show();

	$r1 -> close();

}
// Chiude la connessione al database
$db -> close();

?>